package com.yh.wcx.interceptor;

import cn.hutool.core.bean.BeanUtil;
import com.yh.wcx.entity.User;
import com.yh.wcx.to.UserTo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyInterceptor implements HandlerInterceptor {
    public static ThreadLocal<UserTo> threadLocal=new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截器触发------------");
        HttpSession session = request.getSession();
        User login =(User) session.getAttribute("login");
         if(login!=null){
             UserTo userTo = BeanUtil.copyProperties(login, UserTo.class);
             threadLocal.set(userTo);
             return true;
         }else{
             return false;
         }


    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
